<?php

class porder extends head2d {

 public $showtype = 1;
 
 function porder() {
  $this->table = "porder";
  $this->linetable = "porderline";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[strtoupper($this->table)] = $this;
  $_SESSION["porder"] =$this;
 }
 
 function setdisabled() {
  $this->disabled = '';
 } 
 
 function applyrefs() {
  parent::applyrefs();
  if ($this->header[5] !== "") {
    $desc = freaditem("supplier",$this->header[5]);
    $this->refs[5] = $desc[1];
  }
 }
 
 function emailpdf() {
  $pdf = new showpdf();
  $pdfcode = $pdf->outfile($this);
  $this->emailatt($pdfcode);
 }
 
 function createblank() {
  $this->header = array_fill(0,$this->atts,"");
  $this->header[2] = "0";
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");

  $this->currentline = $this->defaultline();
  $this->currentlinerefs = array_fill(0,$this->lineatts,"");
  $this->currentlinevalids = array_fill(0,$this->lineatts,"");
  $this->currentlinelists = null;
  
  $this->lines = null;
  $this->linerefs = null;
  
  $this->header[18] = dtoiso("");
  $user = authenticate(0);
  $this->header[16] = $user[0]['branch'];
  $this->header[17] = dtoiso("");
  $branchitem = freaditem("branch",$this->header[16]);
  $this->header[6] = $branchitem[5];
  $this->header[7] = $branchitem[6];
  $this->header[8] = $branchitem[7];
  $this->header[9] = $branchitem[8];
  $this->header[10] = $branchitem[9];
 }

 function applyfromrequest() {
  // Mapped requests F1-> field 1 etc, L1->Line 1 etc
  
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");
  $this->currentlinevalids = array_fill(0,$this->lineatts,"");
  
  $i = 5;
	if ($f=@$_REQUEST["f$i"]) {
   if ($f !== $this->header[$i]) {
    // Code of Supplier object
    $desc = freaditem("supplier",$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid supplier code";
     $this->lists[$i] = $this->selectlisth("SUPPLIER",$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }
     
	$i = 1; // short name
  if( $f=@$_REQUEST["f$i"]) {
   $size = $this->metadata[$i]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[$i] = $a;
  }
  
  $i = 19; // long text note
	if( $f=@$_REQUEST["f$i"]) {
   $size = $this->metadata[$i]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[$i] = $a;
  }

  // 6 to 11 are address items
	
	for ($i=6;$i<12;$i++) { 
   if( $f=@$_REQUEST["f$i"]) {
    $size = $this->metadata[$i]->length ;
    $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
    $a = substr($a,0,$size);
    $this->header[6] = $a;
   }
  }
  
  // 17 is required date. 18 is order date
	
	 for ($i=17;$i<19;$i++) {
	  if( $f=@$_REQUEST["f$i"]) {
     // Date. 
     $a = dtoiso($f);
     if ($a) {
      $diff = isotojd()-isotojd($a) ;
      if ($diff > 30 || $diff < -90) {
       $this->valids[$i] = "Date must be within three months of today";
      } else {
       $this->header[$i] = $a;
      }
     } else {
      $this->valids[$i] = "Invalid date";
     }
    }
	 } 
  
  $i = 2;  // Line description
  if( $f=@$_REQUEST["l$i"]) {
   $size = $this->linemetadata[$i]->length ;
   $a = substr(preg_replace("/[^A-Za-z0-9!@#$%&*()+?\-\']/"," ",$f),0,$size);
   $this->currentline[$i] = $a;
  }
  
  $i = 8;  // Note
  if( $f=@$_REQUEST["l$i"]) {
   $size = $this->linemetadata[$i]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*()=+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->currentline[$i] = $a;   
  }
  
  $i = 5; // Product Code
	if($f=@$_REQUEST["l$i"]) { 
    $productitem = freaditem("product",$f);
    if ($productitem == "") {
     $this->currentlinevalids[$i] = "Need valid product code";
     $this->currentlinelists[$i] = $this->selectlisth("product",$f,"l5");
    } else {
     $this->currentline[$i] = $f;
     $this->currentlinerefs[$i] = $productitem[1];
     if ($this->currentline[4] == "") {
      $this->currentline[4] = $productitem[11];
     }
     $this->currentlinelists[$i] = "";
     $this->currentline[2] = $productitem[1];
    }
  }

  $i=3; // Qty
	if( $f=@$_REQUEST["l$i"]) {
   $size = $this->metadata[$i]->length ;
   $dec  = $this->metadata[$i]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->currentline[$i] = $f;
   } else {
    $this->currentlinevalids[$i] = 'Please enter a quantity';
   }
  }
 
  $i = 4; // unit price
	if($f=@$_REQUEST["l$i"]) {
   $size = $this->linemetadata[$i]->length ;
   $dec  = $this->linemetadata[$i]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->currentline[$i] = $f;
   } else {
    $this->currentlinevalids[$i] = "Please enter an amount in currency";
   }
  }
  
  if ($this->header[5] == "") {
   $this->valids[5] = "Cannot leave supplier blank";
  }
  if ($this->header[17] == "") {
   $this->valids[17] = "Cannot leave date blank";
  }
  $this->valid = every($this->valids,""); 

  if ($this->currentline[2] == "") {
   $this->currentlinevalids[2] = "Cannot leave blank";
  }
  if ($this->currentline[4] === "") {
   $this->currentlinevalids[4] = "Please enter line price";
  }
  
   if ($apply = @$_REQUEST["sub"]) {
    if ($apply == "Complete Line" || $apply == "Apply") {
     if ($this->currentline !== $this->defaultline()) {
      if (every($this->currentlinevalids,"") ) {
       $this->lines[$this->currentline[1]] = $this->currentline;
       $this->currentline = $this->defaultline();
       $this->showtype = 1 ;
      } else {
       $this->valid = false;
      }
     }
    } elseif ($apply == "Abandon Changes") {
     $this->showtype = 1;
     $this->currentline = $this->defaultline();
    }
   }

   // Unset if requested. Note that unset is slow
   (isset($_REQUEST["x"])) ? $x=$_REQUEST["x"] : $x = -1;
   
   if (isset($this->lines[$x])) {
    unset($this->lines[$x]);
    $this->lines = array_values($this->lines); // Removes blanks
    $this->currentline = $this->defaultline();
   }

	 if ($this->valid) {
	  $f = @$_REQUEST["showtype"];
	  if ($f <> "-1") {$this->showtype = $_REQUEST["showtype"] ;}
   }

	 if (isset($_REQUEST["editline"]) && (every($this->currentlinevalids,"") || $this->currentline==$this->defaultline())) {
	  $e = $_REQUEST["editline"];
	  if (isset($this->lines[$e])) {
	   $this->currentline = $this->lines[$e];
	   $this->currentline[1] = $e;
     $this->currentlinevalids = array_fill(0,$this->lineatts,"");
     $this->showtype=4;
	  }
	 }
	 
 }
 
 function defaultline() {
  $ans = array_fill(0,$this->lineatts,"");
  $ans[1] = sizeof($this->lines);
  $ans[2] = "";
  $ans[3] = "1";
  $ans[4] = "";
  $ans[5] = "";
  return $ans;
 }
 
 function newcounter() {
  $ans =  getcounter("PO",1);
  return $ans;
 }
 
function makehtml() {
  $co = freaditem("branch",$this->header[16]);
  $cu = freaditem("customer",$this->header[5]);
  $o = "<html>
<head>
<style>
@media print {
td.c1 {font-family:Helvetica;font-size:50px;font-weight:bolder;color:#c0c0c0;}
td.c2 {font-family:Helvetica;font-size:13px;}
td.c3 {font-family:Helvetica;font-size:12px;}
td.c4 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-left:solid 1px gray; }
td.c5 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-right:solid 1px gray; border-left:solid 1px gray}
th.h1 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-left:solid 1px gray; border-top:solid 1px gray}
th.h2 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-left:solid 1px gray; border-right:solid 1px gray; border-top:solid 1px gray}
table {margin-left:50px;margin-top:10px;margin-right:10px;}
}
@media screen {
td {display: none;}
th {display: none;}
table {display: none;}
}
</style>

</head>
<body>
<table width=90%>
<tr><td width=15%>
<img src='coldhot2.jpg' width=100px>
</td>";
  $o .= "<td class=c2 valign=top>";
  $o .= "<b>$co[1]</b><br>";
  $o .= "$co[5]<br>$co[6]<br>$co[7]<br>$co[8]<br>$co[9]<br>";
  $o .= "Tel : $co[11]<br> Email : $co[13]";
  $o .= "</td>";
  $o .= "<td valign=top align=right class=c1>Invoice</td>
</tr></table>

<table width=100%>
 <tr><td width=50%></td><td valign=top align=right class=c3>Invoice Number</td>  <td valign=top  class=c3>";
  $o .= $this->header[0];
  $o .= "</td></tr><tr>
  <td width=50%></td>
  <td valign=top align=right class=c3>Tax Date</td>
  <td valign=top class=c3>";
  $o .= $this->header[17];
  $o .= "</td></tr><tr>
  <td width=50%></td>
  <td valign=top align=right class=c3>Order Number</td>
  <td valign=top class=c3>";
  $o .= $this->header[1];
  $o .= "</td></tr></table><br><br><table width=60%><tr><td class=c3 width=30%>";
  $o .= "$cu[1]<br>$cu[5]<br>$cu[6]<br>";
  $o .= "$cu[7]<br>$cu[8]<br>$cu[9]";
  $o .= "</td><td class=c3 width=30%>";
  $o .= $this->header[1]."<br>";
  $o .= $this->header[5]."<br>";
  $o .= $this->header[6]."<br>";
  $o .= $this->header[7]."<br>";
  $o .= $this->header[8]."<br>";
  $o .= $this->header[9];
  $o .= "</td></tr></table>";
  $o .= "<br><br>";
  $o .= "<table width=100% style='border: 0pt none black;' cellspacing=0; >";
  $o .= "<tr>
 <th valign=top class=h1 align=left width=15%>Code</th>
 <th valign=top class=h1 align=left width=60%>Description</th>
 <th valign=top class=h1 align=right width=15%>Qty</th>
 <th valign=top class=h2 align=right width=10%>Price</th>
</tr>";
 $subtotal = 0;
 $ll = "";
 foreach ($this->lines as $l) {
  $ll = "";
  $ll .= "<tr>";
  $ll .= "<td valign=top class=c4>$l[5]</td>";
  $ll .= "<td valign=top class=c4>$l[2]</td>";
  $ll .= "<td align=right valign=top class=c4>$l[3]</td>";
  $ll .= "<td align=right valign=top class=c4>$l[4]</td>";
  $ll .= "</tr>";
  $o .= $ll;
  $subtotal += $l[4];
 }
 $o .= "<tr><td></td><td></td>";
 $o .= "<td align=right valign=top class=c4>Total</td>";
 $o .= "<td valign=top class=c5 align=right>".sprintf("%2.2f",$subtotal)."</td>";
 $o .= "</tr>";

 $o .= "<tr><td></td><td></td>";
 $o .= "<td align=right valign=top class=c4>GST</td>";
 $o .= "<td valign=top class=c5 align=right>".sprintf("%2.2f",$gst)."</td>";
 $o .= "</tr>";

 $o .= "<tr><td></td><td></td>";
 $o .= "<td align=right valign=top class=c4>Grand Total</td>";
 $o .= "<td valign=top class=c5 align=right>".sprintf("%2.2f",$gst+$subtotal)."</td>";
 $o .= "</tr>";
 $o .= "</table></body></html>";
 return $o;
 }
 
 function writedb() {
  if ($this->disabled=="") {
   parent::writedb();
  }
 }
 
 function showeditaddress() {
  echo "<br>";
  echo "<body onload='document.form.f6.focus()'>";
  echo "<table>";
  echo "<h3>Delivery address</h3>";
  echo $this->htmlrow(6,"Address Line 1");
  echo $this->htmlrow(7,"Address Line 2");
  echo $this->htmlrow(8,"Address Line 3");
  echo $this->htmlrow(9,"Address Line 4");
  echo $this->htmlrow(10,"Post Code");
  echo "</table>";
  if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
   echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  }
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 function showlines() {
  echo "<script>";
    // Ajax calls
  echo "function getprice() {
    obj = document.getElementById('idl5');
    var req = false;
    var self = this;
    self.req = new XMLHttpRequest();
		self.req.open('POST', 'ans.php', true);
    self.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.req.onreadystatechange = function() {
     if (self.req.readyState == 4) {
      document.getElementById('idl4').value =  self.req.responseText.split('^')[0];
      document.getElementById('idl2').value = self.req.responseText.split('^')[2];
  	 }
    }
    self.req.send('a=' + obj.value);
  }";
  echo "</script>";
 
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "</tr></table>";
 	echo "<br>";
	echo "<table width=60%>";
	echo "<tr><th>Actions</th><th>Product Code</th><th width=70%>Description</th><th>Qty</th><th>Unit Price</th><th>Line Price</th></tr>";
	for ($i=0;$i<sizeof($this->lines);$i++) {
	  echo "<tr>";
		echo "<td>";
	  echo "<img title='Edit line' onclick='submitform1($i);' src='images/e.gif'>"; 	
	  if ($this->disabled != 'disabled') {
     echo "<img title='Delete line' onclick=submitform2($i) src='images/x.gif'>";
    }
	  echo "</td>";
	  echo "<td>".$this->lines[$i][5]."</td>";
	  echo "<td>".$this->lines[$i][2]."&nbsp;</td>";
	  echo "<td align=right>".$this->lines[$i][3]."&nbsp;</td>";
	  echo "<td align=right>".sprintf("%2.2f",$this->lines[$i][4])."</td>";
	  echo "<td align=right>".sprintf("%2.2f",$this->lines[$i][4]*$this->lines[$i][3])."</td>";
	  echo "</tr>\n";   
	} 
 	echo "<tr>\n";
 	echo "<td>Add Line</td>";
  if ($this->currentlinelists[5] == "") {
    // It has a description
    echo "<td><input onfocus='dofocus()' onblur=getprice('l5') id=idl5 type=text size=10 maxlength=16 name='l5' value='".$this->currentline[5]."'></td>\n";	
  	if ($this->currentline[5] !== "") {$disable = "readonly=readonly";} else {$disable="";}
  	echo "<td><input onfocus='dofocus()' id=idl2 type=text size=45 maxlength=45 name='l2' value='".$this->currentline[2]."'></td>\n";
  } else {
    //choose from list
    echo "<td colspan=2>".$this->currentlinelists[5]."</td>";
  }
  echo "<td><input id=idl3 type=text size=5 maxlength=5 name='l3' value='".$this->currentline[3]."'></td>\n";	
  echo "<td><input id='idl4' type=text size=5 maxlength=5 name='l4' value='".$this->currentline[4]."'></td>\n";	
  echo "</tr>";
	
	echo "</table>";
	if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
   echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  }
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 function shownote() {
 
  echo "<br>";
  echo "<body onload='document.form.f19.focus()'>";
  echo "<h3>Note</h3>";
  echo "<textarea name=f19 accesskey=T cols=40 rows=6>";
  echo $this->header[19];
  echo "</textarea>";
  echo "<br>";
  if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
   echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  }
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 function showeditline() {
  echo "<br>";
  echo "<body onload='document.form.l3.focus()'>";
  echo "<h3>Editing Line ".($this->currentline[1]+1)."<h3>";
  echo "<table>";
  echo $this->htmlline("2","Description");
  echo $this->htmlline("3","Order Qty");
  echo $this->htmlline("4","Unit Price");
  echo "</table>";
  echo "<h3>Note</h3>";
  echo "<textarea name=l8 accesskey=T cols=40 rows=6>";
  echo $this->currentline[8];
  echo "</textarea>";
  echo "<br>";
  if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value='Apply Line'>";
   echo "<input class=bt accesskey=C type=submit name=sub value='Complete Line'>";
  }
  echo "<input class=bt accesskey=B type=submit name=sub value='Abandon Changes'>";  
 }
 
 function showform() {
  $focalpoint = "f5";
  if ($this->header[5] == "" || $this->valids[5] != "") {$focalpoint = "f5";}
  elseif ($this->header[1] == "" || $this->valids[1] != "") {$focalpoint = "f1";}
  elseif ($this->header[17] == "" || $this->valids[17] != "") {$focalpoint = "f17";}
  elseif ($this->currentline[5] == "") {$focalpoint = "l5";}
  echo "<head>";
  //**************************************************************
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS ;
  // User enters either a product code or description. Script to switch.
  echo "<script>";
  echo "function dofocus() {";
  echo "if (document.form.l5.value != '') {";
  echo " document.form.l3.focus();";
  echo " document.form.l3.select();";
  echo "}";  
  echo "}";
  echo "</script>";
  //**************************************************************
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
  echo "<form name=form method=POST>";
  echo "<input type=hidden name=x value=-1>\n";
  echo "<input type=hidden name=editline value=-1>";
  echo "<input type=hidden name=showtype value=-1>";
  echo "<input type=hidden name=res value=''>";
  echo "<h1>Purchase Order</h1>";
  echo "<table>";
  
  if ($this->header[2] !== "0") {
   echo "<tr>";
   echo " <td>Order Number</td>";
   echo " <td>".$this->header[0]."</td>";
   echo "</tr>";
  }
  echo $this->htmlrow(5,"Supplier");
  echo $this->htmlrow(1,"Order Number");

  // Includes callout date selector
  echo "<tr><td>Delivery Date</td><td>";
  $dt = isotoout($this->header[17]);
  echo "<input $this->disabled id=dot name=f17 size=10 maxlength=10 type=text value='$dt'>\n"; 
  if ($this->disabled != "disabled") {
   $dc = "showChooser(this, 'dot', 'chooserSpan', 1950, 2010, 'd-m-Y', false);";
   echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
   echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
   echo "</div>\n";
  }
  echo "</td>";
  echo "<td>".$this->valids[17]."</td>";  
  echo "</tr>";

  echo "</table>";

  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='h' onclick='document.form.showtype.value=1;document.form.submit()'>Show lines</a></td>";
  echo "<td><a accesskey='l' onclick='document.form.showtype.value=2;document.form.submit();'>Delivery Address</a></td>";
  echo "<td><a accesskey='n' onclick='document.form.showtype.value=3;document.form.submit()'>Show note</a></td>";
  echo "</tr></table>";
  	
  if ($this->showtype==1) {$this->showlines();
  } elseif ($this->showtype==2) {$this->showeditaddress();
  } elseif ($this->showtype==3) {$this->shownote();
  } elseif ($this->showtype==4) {$this->showeditline();
  } else {$this->showlines();
  }
  
  echo "</form>"; 
  // set up help
  echo($this->registerhelp());
 }

}
?>